package cloud

import (
	"encoding/json"
	"github.com/astaxie/beego/logs"
	"reflect"
	"smart_home/api/bots"
	"smart_home/api/bots/depots"
	"smart_home/api/models"
	"strings"
)

type XiaoAi struct {
	bots.BotInfo
	Intent *depots.XiaoAi // 意图
}

func init() {
	bots.AddBots("xiaoai", new(XiaoAi))
}

func (c *XiaoAi) Init() {
	var (
		uid    string
		refVal reflect.Value
		method reflect.Value
		in     = make([]reflect.Value, 0)
		err    error
	)

	c.Intent = &depots.XiaoAi{}
	c.Answer = c.Intent.Answer(`我还不懂您说什么,您可以说"打开插座"`).IsEnd(true)

	if err := json.Unmarshal(c.Body, c.Intent); err != nil {
		logs.Error(err)
		return
	}
	logs.Debug("%+v", c.Intent.Session.User)

	if len(c.Intent.Session.User.AccessToken) == 0 {
		goto NoAuth
	}

	uid = bots.GetToken(c.Intent.Session.User.AccessToken, c.Platform)
	if len(uid) == 0 {
		goto NoAuth
	}

	if c.User, err = models.GetUserById(uid); err != nil {
		goto NoAuth
	}

	refVal = reflect.ValueOf(c)
	method = refVal.MethodByName(c.Intent.Request.SlotInfo.IntentName)
	if method.IsValid() {
		method.Call(in)
	}
	return

NoAuth:
	c.Answer = c.Intent.Answer("请登陆后再操作")
	return
}

// 欢迎意图
func (c *XiaoAi) Mi_Welcome() {
	c.Answer = c.Intent.Answer(`欢迎使用聆漆聆漆智能设备，您可以说"打开插座"`).OpenMic(true)
}

// 无法理解意图
func (c *XiaoAi) Mi_Default() {
	//"小爱没有明白您的意思，请再说一遍吧",
	//"零七没有明白您的意思，请再说一遍吧",
	//`您还在吗？您可以和我说"打开插座"`,

	// 用户无响应
	if c.Intent.Request.NoResponse {
		c.Answer = c.Intent.Answer(`您还在吗？您可以和我说"打开插座"`).OpenMic(true).IsEnd(false)
		return
	}
	c.Answer = c.Intent.Answer(`听不懂您在说什么，您可以和我说"打开插座"`).OpenMic(true).IsEnd(false)
}

// 打开设备
func (c *XiaoAi) Open() {
	answer := bots.SwitchDevice(c.User.Id, c.Intent.GetSlot("equipment").Value, true)
	if len(answer.Message) > 0 {
		c.Answer = c.Intent.Answer(answer.Message).IsEnd(answer.IsEnd).OpenMic(answer.OpenMic)
	}
}

// 关闭设备
func (c *XiaoAi) Close() {
	answer := bots.SwitchDevice(c.User.Id, c.Intent.GetSlot("equipment").Value, false)
	if len(answer.Message) > 0 {
		c.Answer = c.Intent.Answer(answer.Message).IsEnd(answer.IsEnd).OpenMic(answer.OpenMic)
	}
}

// 定时打开
func (c *XiaoAi) TimingOpen() {

	eqName := c.Intent.GetSlot("equipment").Value
	timeSlot := c.Intent.GetSlot("time")
	if timeSlot == nil {
		c.Answer = c.Intent.Answer("请指定定时时间").
			OpenMic(true).
			IsEnd(false).
			AddSessionAttribute("intent_name", "TimingOpen").
			AddSessionAttribute("equipment", eqName)
		return
	}
	time := timeSlot.Value
	timeArr := strings.Split(time, "T")

	res := bots.TimingDevice(c.User.Id, eqName, timeArr[1], timeArr[1], true)
	if len(res.Message) > 0 {
		c.Answer = c.Intent.Answer(res.Message).IsEnd(res.IsEnd).OpenMic(res.OpenMic)
	}
}

// 定时关闭
func (c *XiaoAi) TimingClose() {
	eqName := c.Intent.GetSlot("equipment").Value
	timeSlot := c.Intent.GetSlot("time")
	if timeSlot == nil {
		c.Answer = c.Intent.
			Answer("请指定定时时间").
			OpenMic(true).
			IsEnd(false).
			AddSessionAttribute("intent_name", "TimingClose").
			AddSessionAttribute("equipment", eqName)
		return
	}
	time := timeSlot.Value
	timeArr := strings.Split(time, "T")

	res := bots.TimingDevice(c.User.Id, eqName, timeArr[1], timeArr[1], false)
	if len(res.Message) > 0 {
		c.Answer = c.Intent.Answer(res.Message).IsEnd(res.IsEnd).OpenMic(res.OpenMic)
	}
}

func (c *XiaoAi) Time() {
	logs.Debug("xiaoai body:", string(c.Body))
	logs.Debug("%+v", c.Intent.Session.Attributes)
	intentName := c.Intent.GetSessionAttribute("intent_name")
	if intentName == nil {
		return
	}

	// 定时开关
	if intentName == "TimingOpen" || intentName == "TimingClose" {
		eqName := c.Intent.GetSessionAttribute("equipment")
		if eqName == nil {
			return
		}
		time := c.Intent.GetSlot("time").Value
		timeArr := strings.Split(time, "T")

		isOpen := true
		if intentName == "TimingClose" {
			isOpen = false
		}
		res := bots.TimingDevice(c.User.Id, eqName.(string), timeArr[1], timeArr[1], isOpen)
		if len(res.Message) > 0 {
			c.Answer = c.Intent.Answer(res.Message).IsEnd(res.IsEnd).OpenMic(res.OpenMic)
		}
	}
}
